package com.xggstudio.crm.ui.weiget.screen;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xggstudio.crm.R;

/*
 *  筛选菜单
 *
 *  生成按钮和绑定下拉菜单,事件反馈
 *
 */
public class ScreenLayout extends LinearLayout {

    private Context context;

    public OnItemClickListener listener;

    public ScreenLayout(Context context) {
        super(context);
        init(context);
    }

    public ScreenLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public ScreenLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }


    private void init(Context context ){
        this.context = context;
        setBackgroundColor(Color.WHITE);
    }


    public void setItemButton(String[] array){

        if(array == null) return;

        for (int i = 0; i < array.length; i++) {
            String name = array[i];
            addButton(i,name);
        }

        requestLayout();
    }

    private void addButton(final int id, final String name){

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0
                ,LinearLayout.LayoutParams.MATCH_PARENT);

        params.weight = 1;

        TextView txButton = new TextView(context);
        txButton.setTag(id);
        txButton.setGravity(Gravity.CENTER);
        txButton.setText(name);
        txButton.setCompoundDrawablesWithIntrinsicBounds(null,null,context.getResources().getDrawable(R.drawable.bottom),null);

        txButton.setLayoutParams(params);

        addView(txButton);

        txButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(listener != null){
                    listener.onClick(id,name);
                }
            }
        });

    }

    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }

}
